package com.mall.xiaomi.controller;

import com.mall.xiaomi.pojo.User;
import com.mall.xiaomi.service.LoginService;
import com.mall.xiaomi.util.BeanUtil;
import com.mall.xiaomi.util.CookieUtil;
import com.mall.xiaomi.util.MD5Util;
import com.mall.xiaomi.util.ResultMessage;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author mahaoqi
 * @ClassName LoginController
 * @Description TODO
 * @Date 2021/2/25 14:26
 *
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/user")
public class LoginController {

    @Resource
    private LoginService loginService;
    @Resource
    private ResultMessage resultMessage;
    @Resource
    private RedisTemplate redisTemplate;


    /*
     * @Author mahaoqi
     * @Description 登录
     * @Date 2021/2/26 10:14
     * @Param [user, request, response]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultMessage login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){
        // 返回对象  判断对象是否正确
        user = loginService.login(user);
        // 添加cookie，设置唯一认证
        String encode = MD5Util.MD5Encode(user.getUsername()+user.getPassword(),"UTF-8");
        // 进行加盐
        encode += "|"+user.getUserId()+"|"+user.getUsername()+"|";
        CookieUtil.setCookie(request,response,"XM_TOKEN",encode,1800);
        // 将encode放入redis中，用于认证
        try {
            redisTemplate.opsForHash().putAll(encode, BeanUtil.bean2map(user));
            redisTemplate.expire(encode,30*60, TimeUnit.SECONDS);   //设置过期时间
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 将密码设为null,返回给前端
        user.setPassword(null);
        resultMessage.success("001","登陆成功!",user);
        return resultMessage;
    }

    /*
     * @Author mahaoqi
     * @Description 判断用户名是否唯一
     * @Date 2021/2/26 10:25
     * @Param [username]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @RequestMapping(value = "/username/{username}",method = RequestMethod.GET)
    public ResultMessage username(@PathVariable String username){
        loginService.isUsername(username);
        resultMessage.success("001","可注册!");
        return resultMessage;
    }

    /*
     * @Author mahaoqi
     * @Description 注册
     * @Date 2021/2/26 10:18
     * @Param [user]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ResultMessage register(@RequestBody User user){
        loginService.register(user);
        resultMessage.success("001","注册成功!");
        return resultMessage;
    }


    @RequestMapping(value = "/token",method = RequestMethod.GET)
    public ResultMessage token(@CookieValue("XM_TOKEN")String token,HttpServletRequest request,HttpServletResponse response) throws Exception {
        Map map = redisTemplate.opsForHash().entries(token);
        // 可能map为空 ， 即redis中时间已过期，但是cookie还存在。
        // 这个时候应该删除cookie，让用户重新登录
        if(map.isEmpty()){
            CookieUtil.delCookie(request,token);
            resultMessage.fail("002", "账号过期,请重新登录");
            return resultMessage;
        }
        redisTemplate.expire(token,30*60,TimeUnit.SECONDS);
        User user = BeanUtil.map2bean(map,User.class);
        user.setPassword(null);
        resultMessage.success("001", user);
        return resultMessage;
    }
}
